var server = require('../../utils/server');
var common = require('../../utils/common');
var hideTimer = null;
Page({
  data: {
    orderId: "",
    refundWay: "0",
    reasonList: [{
      "id": 1,
      name: "订单取消失败，重新申请售后"
    }, {
      "id": 2,
      name: "商品质量问题"
    }, {
      "id": 3,
      name: "商品与预期不符/商品有错误"
    }, {
      "id": 4,
      name: "配送速度太慢"
    }, {
      "id": 6,
      name: "优惠券无法使用"
    }, {
      "id": 5,
      name: "其他"
    }],
    refundReason: "",
    reasonId: "",
    orderInfo: [],
    isShowTip: false,
    isRefund: false
  },
  onLoad: function (options) {
    this.setData({
      orderId: options.orderid
    })
    this.getOrderDetail()
  },
  onHide() {
    clearTimeout(hideTimer);
  },
  hideToast() {
    hideTimer = setTimeout(function () {
      wx.hideToast()
    }, 3500)
  },
  bottomAnimate() {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(400).step()
    this.setData({
      animationData: animation.export(),
      showReason: !this.data.showReason
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  selectReason(e) {
    this.setData({
      refundReason: e.currentTarget.dataset.reason,
      reasonId: e.currentTarget.dataset.id
    })
    this.bottomAnimate();
  },
  getOrderDetail() {
    common.getOrdeDetail(this.data.orderId).then(res => {
      var data = res.data.refund_list;
      for (var i = 0; i < res.data.refund_list.length; i++) {
        data[i].refund_num = 0
      }
      res.data.refund_list = data;
      this.setData({
        orderInfo: res.data
      })
    })
  },
  switchNav(e) {
    var refundWay = e.currentTarget.dataset.status;
    this.setData({
      refundWay: refundWay
    })
  },
  confirmPop() {
    var goodsList = this.data.orderInfo.refund_list,
      newGoodsArray = [];
    if (this.data.refundWay == "0") {
      for (var i = 0; i < goodsList.length; i++) {
        newGoodsArray.push({
          "goods_id": goodsList[i].goods_id,
          "quantity": goodsList[i].quantity
        })
      }
    } else {
      for (var i = 0; i < goodsList.length; i++) {
        if (goodsList[i].refund_num != 0) {
          newGoodsArray.push({
            "goods_id": goodsList[i].goods_id,
            "quantity": goodsList[i].refund_num
          })
        }
      }
    }
    if (newGoodsArray.length > 0 && this.data.reasonId != "") {
      this.setData({
        showPop: !this.data.showPop,
        newGoodsArray: newGoodsArray
      })

    } else if (newGoodsArray.length == 0) {
      common.showToast("请选择需要售后的商品！");
      this.hideToast();
    } else if (this.data.reasonId == "") {
      this.bottomAnimate();
      common.showToast("请选择退款原因！");
      this.hideToast();
    }
  },
  submitApply() {
    this.setData({
      isRefund: true
    })
    server.postJSON(common.storePath + 'refund/add/', {
      "order_id": this.data.orderInfo.order_id,
      "store_id": this.data.orderInfo.store.store_id,
      "type": this.data.refundWay,
      "goods": this.data.newGoodsArray,
      "reason_id": this.data.reasonId
    }, res => {
      if (res.data.errno == "0") {
        wx.redirectTo({
          url: 'orderRefundDetail?orderid=' + this.data.orderInfo.order_id
        })
        this.setData({
          showPop: !this.data.showPop,
          isRefund: false
        })
      } else {
        common.showErrorToast(res.data.errmsg);
        this.setData({
          showPop: !this.data.showPop,
          isRefund: false
        })
      }
    })
  },
  addNum(e) {
    var goodsIndex = e.currentTarget.dataset.index,
      orderInfo = this.data.orderInfo;
    if (orderInfo.refund_list[goodsIndex].refund_num < parseInt(orderInfo.refund_list[goodsIndex].quantity)) {
      orderInfo.refund_list[goodsIndex].refund_num = orderInfo.refund_list[goodsIndex].refund_num + 1;
      this.setData({
        orderInfo: orderInfo
      })
    } else {
      common.showToast("退货数量超出购买数量！");
      this.hideToast();
    }

  },
  reduceNum(e) {
    var goodsIndex = e.currentTarget.dataset.index,
      orderInfo = this.data.orderInfo;
    orderInfo.refund_list[goodsIndex].refund_num = orderInfo.refund_list[goodsIndex].refund_num - 1;
    this.setData({
      orderInfo: orderInfo
    })

  },
  hideTiP() {
    this.setData({
      isShowTip: !this.data.isShowTip
    })
  }
})